package com.beeasy.hzbpm.ctrl;

import com.beeasy.hzbpm.bean.Lock;
import com.github.llyb120.nami.ctrl.Ctrl;
import com.github.llyb120.json.Arr;
import com.github.llyb120.json.Obj;

import static com.beeasy.hzbpm.bean.MongoService.db;
import static com.github.llyb120.json.Json.*;

public class sync extends Ctrl {

    public void sync(){
        synchronized (Lock.lockMap){
            Obj umap = o();
            Arr user = aaa($request.aa("user")
                .stream()
                .map(ee -> {
                    Obj e = ooo(ee);
                    Obj item = o(
                        "id", e.s("id"),
                        "username", e.s("username"),
                        "password", e.s("password"),
                        "su", e.ii("su"),
                        "true_name", e.s("true_name"),
                        "phone", e.ss("phone"),
                        "orgs", a()
                    );
                    umap.put(e.s("id"), item);
                    return item;
                })
                .toArray());

            Arr dep = aaa(
                $request.aa("dep")
                .stream()
                .map(ee -> {
                    Obj e = ooo(ee);
                    return o(
                        "id", e.ss("id"),
                        "name", e.ss("name"),
                        "pid", e.s("pid", null),
                        "sort", e.ii("sort")
                    );
                })
                .toArray()
            );

            $request.aa("user_dep")
                .forEach(ee -> {
                    Obj e = ooo(ee);
                    umap
                        .oo(e.ss("uid"))
                        .aa("orgs")
                        .add(o(
                            "id", e.ss("id"),
                            "type", e.ss("type")
                        ));
                });

            Arr role = aaa(
                $request.aa("role")
                    .stream()
                    .map(ee -> {
                        Obj e = ooo(ee);
                        return o(
                            "id", e.ss("id"),
                            "name", e.ss("name"),
                            "pid", e.s("pid", null),
                            "sort", e.ii("sort")
                        );
                    })
                    .toArray()
            );
            $request.aa("user_role")
                .forEach(ee -> {
                    Obj e = ooo(ee);
                    umap
                        .oo(e.ss("uid"))
                        .aa("orgs")
                        .add(o(
                            "id", e.ss("id"),
                            "type", e.ss("type")
                        ));
                });

            db.getCollection("user")
                .deleteMany(o().toBson());
            db.getCollection("user")
                .insertMany(user.toBson());
            db.getCollection("dep")
                .deleteMany(o().toBson());
            db.getCollection("dep")
                .insertMany(dep.toBson());
            db.getCollection("role")
                .deleteMany(o().toBson());
            db.getCollection("role")
                .insertMany(role.toBson());
        }
    }



}
